#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
try:
    import ujson as json
except:
    import json
from controls import data_user_brief_mysql
from controls import data_user_detail_mysql
from controls import data_black_user_mysql
from controls import data_img_base_mysql
from utils.file_util import file_upload
from utils import log

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("username")

class GetAllUser(BaseHandler):
    def post(self, *args, **kwargs):
        begin = self.get_argument('begin')
        limit = self.get_argument('limit')

        if not (begin and limit):
            log.i('params fetching error')
            res = {'ret':1, 'msg':'参数获取错误'}
            self.write(json.dumps(res))
            return

        black_userid_list = data_black_user_mysql.get_black_user_all()
        black_user_count = data_black_user_mysql.get_black_user_count()
        all_user_count = data_user_brief_mysql.get_user_count()

        log.i(black_user_count)
        log.i(all_user_count)

        user_brief = data_user_brief_mysql.get_all_user_brief_except_black(begin, limit, black_userid_list)
        normal_user_count = all_user_count - black_user_count

        if user_brief:
            res = {'ret':0, 'msg':'ok', 'res': {
                'total': normal_user_count,
                'begin': begin,
                'limit': limit,
                'data': user_brief}}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret':1, 'msg':'获取用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

class GetAllBlackUser(BaseHandler):
    def post(self, *args, **kwargs):
        begin = self.get_argument('begin')
        limit = self.get_argument('limit')
        if not (begin and limit):
            log.i('params fetching error')
            res = {'ret':1, 'msg':'参数获取错误'}
            self.write(json.dumps(res))
            self.finish()
            return
        black_userid_list = data_black_user_mysql.get_black_user_all_with_begin_limit(begin, limit)
        user_brief = data_user_brief_mysql.get_user_brief_by_userid_list(black_userid_list)
        black_user_count = data_black_user_mysql.get_black_user_count()
        if user_brief or user_brief == []:
            res = {'ret':0, 'msg':'ok', 'res': {
                'total': black_user_count,
                'data': user_brief,
                'begin': begin,
                'limit': limit
            }}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret':1, 'msg':'获取用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

class MarkUserAsBlack(BaseHandler):
    def post(self, *args, **kwargs):
        userid = self.get_argument('userid')
        if not userid:
            log.i('params fetching error')
            res = {'ret':1, 'msg':'参数获取错误'}
            self.write(json.dumps(res))
            return

        item = {'userid': userid}

        if data_black_user_mysql.add_black_user_item(item):
            res = {'ret':0, 'msg':'ok'}
            self.write(json.dumps(res))
            return
        else:
            res = {'ret':1, 'msg':'添加用户至黑名单失败'}
            self.write(json.dumps(res))
            return

class MarkUserAsNormal(BaseHandler):
    def post(self, *args, **kwargs):
        userid = self.get_argument('userid')
        if not userid:
            log.i('params fetching error')
            res = {'ret':1, 'msg':'参数获取错误'}
            self.write(json.dumps(res))
            return
        if data_black_user_mysql.drop_black_user_by_userid(userid):
            res = {'ret':0, 'msg':'ok'}
            self.write(json.dumps(res))
            return
        else:
            res = {'ret':1, 'msg':'解除用户黑名单失败'}
            self.write(json.dumps(res))
            return

class UpdateOneUserHandler(BaseHandler):
    def get(self, *args, **kwargs):
        userid = self.get_argument('userid')
        user_brief = data_user_brief_mysql.get_user_brief_by_userid(userid)
        user_detail = data_user_detail_mysql.get_user_detail_by_userid(userid)

        item = {'userid': user_brief['userid'],
                'name': user_brief['name'],
                'intro': user_brief['intro'],
                'zone': user_brief['zone'],
                'imglink': user_brief['imglink'],
                'sex': user_detail['sex'],
                'sexot': user_detail['sexot'],
                'love': user_detail['love'],
                'horo': user_detail['horo']
        }

        if item:
            res = {'ret': 0, 'msg': 'ok', 'res': item}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '获取用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

    def post(self, *args, **kwargs):
        userid = self.get_argument('userid')
        name = self.get_argument('name')
        intro = self.get_argument('intro')
        sex = self.get_argument('sex')
        sexot = self.get_argument('sexot')
        love = self.get_argument('love')
        horo = self.get_argument('horo')

        if not (userid and name and sex and sexot and love and horo and intro):
            log.i('params fetching error')
            self.redirect('/cms/manageuser')
            return

        file = self.request.files['file'][0]
        file_res = file_upload(file)

        if not (file_res and file_res['ret'] == 0):
            res = {'ret': 1, 'msg': '保存图片失败'}
            self.write(json.dumps(res))
            self.finish()
            return

        imglink = data_img_base_mysql.get_img_url_by_imgid(file_res['res']['imgid'])

        detail_item = {
            'userid': userid,
            'sex': sex,
            'sexot': sexot,
            'love': love,
            'horo': horo
        }

        brief_item = {
            'userid': userid,
            'name': name,
            'intro': intro,
            'imglink': imglink
        }

        detail = data_user_detail_mysql.update_fake_user_detail_by_userid(userid, detail_item)
        brief = data_user_brief_mysql.update_fake_user_brief_by_userid(userid, brief_item)

        if detail and brief:
            res = {'ret': 0, 'msg': '用户资料更新成功'}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '用户资料更新失败'}
            self.write(json.dumps(res))
            self.finish()
            return

class GetNormalUserCount(BaseHandler):
    def post(self, *args, **kwargs):
        black_user_count = data_black_user_mysql.get_black_user_count()
        user_count = data_user_brief_mysql.get_user_count()
        notmal_user_count = user_count - black_user_count
        res = {'ret': 0, 'msg': 'ok', 'res':int(notmal_user_count)}
        self.write(json.dumps(res))
        self.finish()
        return

class GetBlackUserCount(BaseHandler):
    def post(self, *args, **kwargs):
        black_user_count = data_black_user_mysql.get_black_user_count()
        res = {'ret': 0, 'msg': 'ok', 'res':int(black_user_count)}
        self.write(json.dumps(res))
        self.finish()
        return